// page/map/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mapCtx: null,
    scale: 15,
    latitude: "",
    longitude: "",
    markers: [],
    listData: [
      {
        "id": 1,
        "placeName": "中关村广场",
        "placeImageUrl": "",
        "placeOpenTime": 1505854800,
        "placeCloseTime": 1505919600,
        "placeAddress": "北京市海淀区中关广场",
        "placeLongitude": "116.303900",
        "placeLatitude": "39.976460"
      },
      {
        "id": 2,
        "placeName": "虎丘的广场",
        "placeImageUrl": "",
        "placeOpenTime": 1506286800,
        "placeCloseTime": 1506258000,
        "placeAddress": "江苏省苏州虎丘",
        "placeLongitude": "120.410770",
        "placeLatitude": "31.325370"
      },
      {
        "id": 3,
        "placeName": "东城区广场",
        "placeImageUrl": "",
        "placeOpenTime": 1506200400,
        "placeCloseTime": 1506265200,
        "placeAddress": "北京市东城区广场",
        "placeLongitude": "116.407526",
        "placeLatitude": "39.904030"
      },
      {
        "id": 4,
        "placeName": "回龙观广场",
        "placeImageUrl": "",
        "placeOpenTime": 1506243600,
        "placeCloseTime": 1506265200,
        "placeAddress": "北京市昌平区回龙观东大街",
        "placeLongitude": "116.362300",
        "placeLatitude": "40.083480"
      },
      {
        "id": 5,
        "placeName": "吴中的广场",
        "placeImageUrl": "",
        "placeOpenTime": 1506286800,
        "placeCloseTime": 1506351600,
        "placeAddress": "江苏苏州吴中",
        "placeLongitude": "120.416152",
        "placeLatitude": "31.322980"
      }
    ],
  },

  controltap(e) {
    this.mapCtx.moveToLocation();
  },

  strSub(a) {
    var str = a.split(".")[1];
    str = str.substring(0, str.length - 1)
    return a.split(".")[0] + '.' + str;
  },

  createMarker(point) {
    // let latitude = this.strSub(point.placeLatitude);
    let latitude = point.placeLatitude;
    console.log(latitude, "[--latitude")
    let longitude = point.placeLongitude;
    let marker = {
      // iconPath: "../../image/banner5.jpeg",
      id: point.id || 0,
      name: point.placeName || '',
      title: point.placeName || '',
      latitude: latitude,
      longitude: longitude,
      label:{
        x:-24,
        y:-26,
        content: point.placeName
      },
      width: 30,
      height: 30
    };
    return marker;
  },

  fetchSchoolMarkers() {
    var market = [];
    for (let item of this.data.listData) {
      let marker1 = this.createMarker(item);
      market.push(marker1)
    }
    return market;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _this = this;

    wx.getLocation({
      type: "wgs84", // 返回可以用于 wx.openLocation 的经纬度
      success: (res) => {
        _this.setData({
          markers: _this.fetchSchoolMarkers(),
          scale: 12,
          longitude: res.longitude,
          latitude: res.latitude
        })
      }
    });

    wx.getSystemInfo({
      success: function (res) {
        //设置 map 高度，根据当前设备宽高满屏显示
        _this.setData({
          view: {
            Height: res.windowHeight
          },
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 使用 wx.createMapContext 获取 map 上下文 
    this.mapCtx = wx.createMapContext("map");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})